﻿using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NewRetail.Application.Components.Platform.Entity;
using NewRetail.Application.Components.WeChat.CardVoucher.Model;
using NewRetail.Application.Components.WeChat.Member.Entity;
using NewRetail.Application.Components.WeChat.Member.Model;
using NewRetail.Application.Components.WeChat.Member.Services;
using NewRetail.Application.Core;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
using System;
using System.Threading.Tasks;

namespace NewRetail.Api.Controllers.WeChartControllers
{
    [NewRetailRoute("wechat-member")]
    public class WeChatMemberController : BaseController
    {
        #region Field
        public readonly IMemberService _wechartMemberService;
        private readonly ILogger _logger;
        #endregion

        #region Constructor
        public WeChatMemberController(IMemberService wechartMemberService, ILogger<WeChatMemberController> logger)
        {
            _wechartMemberService = wechartMemberService;
            _logger = logger;
        }
        #endregion

        #region method

        #region 根据openId获取微信用户信息
        /// <summary>
        /// 根据openId获取微信用户信息
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="appId"></param>
        /// <returns></returns>
        [HttpPost("query-wechatuser-byopenid")]
        public async Task<ResponseEntity<WeChatMember>> GetWeChatUserInfoByOpenId(string openId, string appId)
        {
            var result = await _wechartMemberService.GetUserInfoByOpenId(openId, appId);

            return new ResponseEntity<WeChatMember>().SetData(result);
        }
        #endregion

        #region 根据openId获取本地数据库用户信息
        /// <summary>
        /// 根据openId获取本地数据库用户信息
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        [HttpGet("query-localuser-byopenid")]
        public async Task<ResponseEntity<PosMember>> GetLocalUserInfoByOpenId(string openId)
        {
            var ret = new ResponseEntity<PosMember>();
            try
            {
                if(string.IsNullOrEmpty(openId))
                    ret.SetError("openId不能为空！");
                var result = await _wechartMemberService.GetLocalUserInfoByOpenId(openId);
                ret.SetData(result);
            }
            catch (Exception e)
            {
                ret.SetError("查询用户信息失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 获取用户列表
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="nextOpenId"></param>
        /// <param name="appId"></param>
        /// <returns></returns>
        [HttpPost("query-wechatusers")]
        public async Task<ResponseEntity<WeChatUserListDto>> GetWeChatUsers(string appId, string nextOpenId = null)
        {
            var result = new WeChatUserListDto();
            try
            {
                result = await _wechartMemberService.GetUserList(appId, nextOpenId);
            }
            catch(Exception ex)
            {
                _logger.LogError(ex.Message);
            }
            return new ResponseEntity<WeChatUserListDto>().SetData(result);
        }
        #endregion

        #region 测试会员插入
        /// <summary>
        /// 测试会员插入
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="appId"></param>
        /// <returns></returns>
        [HttpPost("queryadd-wechatuser"), Obsolete]
        public async Task<ResponseEntity<bool>> GetWeChatUserInfoByOpenId(WeChatMemberVerAdd weChatMemberVerAdd)
        {
            var result = await _wechartMemberService.VerifyMemberAdd(weChatMemberVerAdd);

            return new ResponseEntity<bool>().SetData(result);
        }
        #endregion

        #region 会员卡相关

        #region 创建会员卡
        /// <summary>
        /// 创建会员卡
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>

        [HttpPost("wechat-createcard")]
        public async Task<ResponseEntity<string>> CreateWeChatCard([FromBody] WeChatCardCreateInputDto inputDto)
        {
            inputDto.Should().NotBeNull();
            var ret = new ResponseEntity<string>();
            try
            {
                var result = await _wechartMemberService.CreateWeChatCardAsync(inputDto);
                if (string.IsNullOrEmpty(result.Item1))
                    ret.SetError("创建会员卡失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("创建会员卡失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 批量查询卡券列表
        /// <summary>
        /// 批量查询卡券列表
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>

        [HttpPost("wechat-querycardlist")]
        public async Task<ResponseEntity<CardBatchGetResponse>> QueryWeChatCardList([FromBody] WeChatCardListQueryInputDto inputDto)
        {
            inputDto.Should().NotBeNull();
            var ret = new ResponseEntity<CardBatchGetResponse>();
            try
            {
                var result = await _wechartMemberService.QueryWeChatCardListAsync(inputDto);
                if (result.Item1 == null)
                    ret.SetError("批量查询卡券列表失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("批量查询卡券列表失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 查看卡券详情
        /// <summary>
        /// 查看卡券详情
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>

        [HttpPost("wechat-querycarddetail")]
        public async Task<ResponseEntity<CardGetResponse>> QueryWeChatCardDetail([FromBody] CardDetailQueryInputDto inputData)
        {
            inputData.Should().NotBeNull();
            var ret = new ResponseEntity<CardGetResponse>();
            try
            {
                var result = await _wechartMemberService.QueryWeChatCardDetailAsync(inputData.CardId, inputData.AppId);
                if (result.Item1 == null)
                    ret.SetError("查看卡券详情失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("查看卡券详情失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 删除卡券
        /// <summary>
        /// 删除卡券
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>

        [HttpPost("wechat-deletecard")]
        public async Task<ResponseEntity<CardDeleteResponse>> DeleteWeChatCard([FromBody] WeChatCardDeleteInputDto inputDto)
        {
            inputDto.Should().NotBeNull();
            var ret = new ResponseEntity<CardDeleteResponse>();
            try
            {
                var result = await _wechartMemberService.DeleteWeChatCardAsync(inputDto);
                if (result.Item1 == null)
                    ret.SetError("删除卡券失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("删除卡券失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 创建卡券二维码
        /// <summary>
        /// 创建卡券二维码
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>

        [HttpPost("wechat-createcardqrcode")]
        public async Task<ResponseEntity<CardQrcodeCreateResponse>> CreateWeChatCardQrCode([FromBody] CardQrcodeCreateInputDto inputData)
        {
            inputData.Should().NotBeNull();
            var ret = new ResponseEntity<CardQrcodeCreateResponse>();
            try
            {
                var result = await _wechartMemberService.CreateWeChatCardQrCodeAsync(inputData);
                if (result.Item1 == null)
                    ret.SetError("创建卡券二维码失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("创建卡券二维码失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 获取开卡插件参数 调用此接口后返回的url可以直接打开到领取卡界面
        /// <summary>
        /// 获取开卡插件参数
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>

        [HttpPost("wechat-cardmembercardactivate")]
        public async Task<ResponseEntity<CardMemberCardActivateGetUrlResponse>> GetCardMemberCardActivate([FromBody] CardMemberCardActivateInputDto inputData)
        {
            inputData.Should().NotBeNull();
            var ret = new ResponseEntity<CardMemberCardActivateGetUrlResponse>();
            try
            {
                var result = await _wechartMemberService.GetCardMemberCardActivateAsync(inputData);
                if (result.Item1 == null)
                    ret.SetError("获取开卡插件参数失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("获取开卡插件参数失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 设置测试白名单
        /// <summary>
        /// 设置测试白名单
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>

        [HttpPost("wechat-cardmembertestwhitelist")]
        public async Task<ResponseEntity<CardTestWhiteListSetResponse>> SetCardMemberTestWhiteList([FromBody] CardMemberTestWhiteListInputDto inputData)
        {
            inputData.Should().NotBeNull();
            var ret = new ResponseEntity<CardTestWhiteListSetResponse>();
            try
            {
                var result = await _wechartMemberService.SetCardMemberTestWhiteListAsync(inputData);
                if (result.Item1 == null)
                    ret.SetError("设置测试白名单失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("设置测试白名单失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 拉取会员信息（积分查询）接口
        /// <summary>
        /// 拉取会员信息（积分查询）接口
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>

        [HttpPost("wechat-getcardmemberuserinfo")]
        public async Task<ResponseEntity<CardMemberCardUserInfoGetResponse>> GetCardMemberUserInfo([FromBody] CardMemberUserInfoInputDto inputData)
        {
            inputData.Should().NotBeNull();
            var ret = new ResponseEntity<CardMemberCardUserInfoGetResponse>();
            try
            {
                var result = await _wechartMemberService.GetCardMemberUserInfoAsync(inputData);
                if (result.Item1 == null)
                    ret.SetError("拉取会员信息（积分查询）失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("拉取会员信息（积分查询）失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 解码code
        /// <summary>
        /// 解码code
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>

        [HttpPost("wechat-cardmemberdecryptcode")]
        public async Task<ResponseEntity<CardCodeDecryptResponse>> CardMemberDecryptCode([FromBody] CardMemberDecryptCodeInputDto inputData)
        {
            inputData.Should().NotBeNull();
            var ret = new ResponseEntity<CardCodeDecryptResponse>();
            try
            {
                var result = await _wechartMemberService.CardMemberDecryptCodeAsync(inputData);
                if (result.Item1 == null)
                    ret.SetError("解码code失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("解码code失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 查询code
        /// <summary>
        /// 查询code
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>

        [HttpPost("wechat-cardmemberquerycode")]
        public async Task<ResponseEntity<CardCodeGetResponse>> CardMemberQueryCode([FromBody] CardMemberQueryCodeInputDto inputData)
        {
            inputData.Should().NotBeNull();
            var ret = new ResponseEntity<CardCodeGetResponse>();
            try
            {
                var result = await _wechartMemberService.CardMemberQueryCodeAsync(inputData);
                if (result.Item1 == null)
                    ret.SetError("查询code失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("查询code失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 获取用户已领取卡券
        /// <summary>
        /// 获取用户已领取卡券
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>

        [HttpPost("wechat-cardusercardlist")]
        public async Task<ResponseEntity<CardUserGetCardListResponse>> CardUserGetCardList([FromBody] WeChatCardUserCardInputDto inputDto)
        {
            inputDto.Should().NotBeNull();
            var ret = new ResponseEntity<CardUserGetCardListResponse>();
            try
            {
                var result = await _wechartMemberService.CardUserGetCardListAsync(inputDto);
                if (result.Item1 == null)
                    ret.SetError("获取用户已领取卡券失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("获取用户已领取卡券失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 更新会员信息(积分信息)
        /// <summary>
        /// 更新会员信息(积分信息)
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>

        [HttpPost("wechat-updateusermembercard")]
        public async Task<ResponseEntity<CardMemberCardUpdateUserResponse>> UpdateWeChatUserMemberCardAsync([FromBody] UpdateWeChatUserMemberCardInputDto inputDto)
        {
            inputDto.Should().NotBeNull();
            var ret = new ResponseEntity<CardMemberCardUpdateUserResponse>();
            try
            {
                var result = await _wechartMemberService.UpdateWeChatUserMemberCardAsync(inputDto);
                if (result.Item1 == null)
                    ret.SetError("更新会员信息(积分信息)失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("更新会员信息(积分信息)失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #endregion

        #endregion
    }
}
